/**
 * Copyright(C) 2012 Luvina Software Company
 *
 * KatakanaValidator.java, 10/1/2013, bacdv
 */
package net.luvina.manageuser.validator;

import java.text.MessageFormat;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import net.luvina.manageuser.util.Message;
import net.luvina.manageuser.util.StringUtil;

/**
 * KatakanaValidator
 *
 * @author bacdv
 *
 */
public class KatakanaValidator implements Validator {

    /*
     * (non-Javadoc)
     *
     * @see
     * javax.faces.validator.Validator#validate(javax.faces.context.FacesContext
     * , javax.faces.component.UIComponent, java.lang.Object)
     */
    public void validate(FacesContext context, UIComponent component, Object obj)
            throws ValidatorException {
        int max = 255;
        String nameKana = (String) obj;
        String labelItem = (String) component.getAttributes().get("label");
        if ((nameKana != null) && (nameKana.length() > 0)
                && (!StringUtil.isKatakana(nameKana))) {
            FacesMessage message = new FacesMessage();
            message.setSummary(MessageFormat.format(Message.getString("ER009"),
                    labelItem));
            throw new ValidatorException(message);
        } else {
            if (nameKana.length() > max) {
                FacesMessage message = new FacesMessage();
                message.setSummary(MessageFormat.format(Message
                        .getString("ER006"), max, labelItem));
                throw new ValidatorException(message);
            }
        }
    }

}
